using System;
using System.Collections;
using System.Collections.Generic;
using Holoville.HOTween;
using Holoville.HOTween.Core;
using UnityEngine;

public class BonuseItem : SynchronizedGameObject
{
	public int idSorting;

	public idBonuse id;

	public typeGroupBonuse typeGroup;

	public List<SpawnBonuse> listSpawnBonus;

	public string idProduct;

	public bool canGetInCar;

	public int verAddThisBonuse = 10;

	public int totalVerGroup = -1;

	public int countItemAddMin = 1;

	public int countItemAddMax = 2;

	public bool UseTimerToDestroy;

	public bool destroyWhenCollection;

	public float TimeToDestroy;

	private float _timeToDestroy;

	public bool collectAnyway;

	public bool checkOnGround;

	public GameObject soundObject;

	public MeshRenderer mesh;

	private bool _isKilled;

	private bool _isGet;

	public bool isVisible = true;

	public bool changeBonuse;

	[SerializeField]
	private List<BonusItemColor> colors;

	[SerializeField]
	private List<IDBonusGameObject> bonusTextures;

	private Vector3 endPos;

	private Tweener tweenPosition;

	[HideInInspector]
	public NJGMapItem iconMiniMap;

	[SerializeField]
	private GameObject[] oreol;

	public bool active = true;

	public GameObject[] children;

	public float respwnTime = 10f;

	public bool IsKilled
	{
		get
		{
			return _isKilled;
		}
		set
		{
			_isKilled = value;
		}
	}

	public override void Awake()
	{
		iconMiniMap = GetComponent<NJGMapItem>();
	}

	public override void Start()
	{
		if (GameController.instance != null && GameController.instance.curTypeGame == TypeGame.Zombe && id == idBonuse.UpWeaponInZombie && iconMiniMap != null)
		{
			iconMiniMap.SetType("BonusItem");
			iconMiniMap.enabled = true;
		}
		HOTween.Init();
		base.transform.parent = bonuseManager.thisScript.objSpisokAllBonuse.transform;
		bonuseManager.thisScript.AddBonuseToList(this);
		if (!PhotonNetwork.isMasterClient && base.photonView != null && base.photonView.instantiationData != null)
		{
			Dictionary<string, object> dictionary = base.photonView.instantiationData[0] as Dictionary<string, object>;
			SpawnBonuse item = default(SpawnBonuse);
			for (int i = 0; i < dictionary.Count; i++)
			{
				changeBonuse = true;
				Dictionary<string, object> dictionary2 = (Dictionary<string, object>)dictionary["k" + i];
				object value;
				dictionary2.TryGetValue("count", out value);
				object value2;
				dictionary2.TryGetValue("idProduct", out value2);
				object value3;
				dictionary2.TryGetValue("percent", out value3);
				object value4;
				dictionary2.TryGetValue("spawnType", out value4);
				item.count = Convert.ToInt32(value);
				item.idProduct = value2.ToString();
				item.percent = Convert.ToInt32(value3);
				item.spawnType = (idBonuse)Convert.ToInt32(value4);
				listSpawnBonus.Add(item);
			}
		}
		OnOreol();
		endPos = base.transform.position;
		_timeToDestroy = TimeToDestroy;
		isVisible = false;
		if (UseTimerToDestroy)
		{
			StartCoroutine(CheckOnGround());
			if (settings.offlineMode || base.photonView.isMine)
			{
				StartCoroutine(WaitForRemove());
			}
		}
		SetAnimation(bonuseManager.thisScript.materialRed, bonuseManager.thisScript.materialBlue, bonuseManager.thisScript.materialOrange, bonuseManager.thisScript.materialGreen);
	}

	private void OnTriggerEnter(Collider other)
	{
		if (!active)
		{
			return;
		}
		InstSound();
		Mortal componentInParent = other.GetComponentInParent<Mortal>();
		if (componentInParent == null)
		{
			return;
		}
		if (componentInParent is EntityBehavior)
		{
			EntityBehavior entityBehavior = componentInParent as EntityBehavior;
			if (entityBehavior == GameController.instance.playerScript.currentCar && bonuseManager.thisScript.getBonuse(other.gameObject, true, this) && UseTimerToDestroy)
			{
				active = false;
			}
		}
		else if (componentInParent == PlayerBehavior.MyPlayer && bonuseManager.thisScript.getBonuse(other.gameObject, false, this) && UseTimerToDestroy)
		{
			active = false;
		}
	}

	private IEnumerator WaitForRemove()
	{
		yield return new WaitForSeconds(_timeToDestroy);
		DisableBonuse();
	}

	public void DisableBonuse()
	{
		GameObject[] array = children;
		foreach (GameObject gameObject in array)
		{
			gameObject.SetActive(false);
		}
		if (settings.offlineMode || base.photonView == null)
		{
			if (UseTimerToDestroy || destroyWhenCollection)
			{
				UnityEngine.Object.Destroy(base.gameObject);
				return;
			}
			BonuseItem bonuseItem = RandomBonuse();
			StartCoroutine(DisableForTime((int)bonuseItem.id, bonuseItem.idProduct, bonuseItem.countItemAddMin));
		}
		else if ((GameController.instance.curTypeGame == TypeGame.Zombe && id == idBonuse.UpWeaponInZombie) || (GameController.instance.curTypeGame == TypeGame.CopsAndRobbers && id == idBonuse.RobberInventory))
		{
			UnityEngine.Object.Destroy(base.gameObject);
		}
		else
		{
			BonuseItem bonuseItem2 = RandomBonuse();
			base.photonView.RPC("DestroyRPC", PhotonTargets.All, (int)bonuseItem2.id, bonuseItem2.idProduct, bonuseItem2.countItemAddMin);
		}
	}

	public void ShowGetBonusAnimation()
	{
		switch (id)
		{
		case idBonuse.Ammo:
			FlyingPointsController.instance.AddFlyingPoints(base.transform.position, PointsType.Ammo, 0, true);
			break;
		case idBonuse.Health:
			FlyingPointsController.instance.AddFlyingPoints(base.transform.position, PointsType.Healts, countItemAddMin, true);
			break;
		case idBonuse.Respect:
			FlyingPointsController.instance.AddFlyingPoints(base.transform.position, PointsType.Reputation, countItemAddMin, true);
			break;
		case idBonuse.Jetpack:
			FlyingPointsController.instance.AddFlyingPoints(base.transform.position, PointsType.Jetpack, 0, true);
			break;
		}
	}

	[PunRPC]
	public void DestroyRPC(int iBonuse, string iProduct, int iCount)
	{
		if (UseTimerToDestroy)
		{
			if (base.photonView.isMine)
			{
				_isGet = true;
				if (tweenPosition != null)
				{
					HOTween.Kill(tweenPosition);
				}
				PhotonNetwork.RemoveRPCs(base.photonView);
				PhotonNetwork.Destroy(base.gameObject);
			}
			else
			{
				if (tweenPosition != null)
				{
					HOTween.Kill(tweenPosition);
				}
				IsKilled = true;
				base.transform.position = new Vector3(0f, -10000f, 0f);
			}
		}
		else
		{
			StartCoroutine(DisableForTime(iBonuse, iProduct, iCount));
		}
	}

	public void SetVisible(bool val)
	{
		if (isVisible != val)
		{
			for (int i = 0; i < base.transform.childCount; i++)
			{
				base.transform.GetChild(i).gameObject.SetActive(val);
			}
			isVisible = val;
		}
	}

	private void InstSound()
	{
		if (_isGet && soundObject != null)
		{
			GameObject gameObject = UnityEngine.Object.Instantiate(soundObject, base.transform.position, Quaternion.identity) as GameObject;
			if (gameObject != null)
			{
				gameObject.transform.parent = base.transform.parent;
			}
		}
	}

	private void Disabled(bool value)
	{
		GameObject[] array = children;
		foreach (GameObject gameObject in array)
		{
			gameObject.SetActive(!value);
		}
		active = !value;
	}

	private IEnumerator DisableForTime(int iBonuse, string iProduct, int iCount)
	{
		Disabled(true);
		if (changeBonuse)
		{
			ChangeBonuse(iBonuse, iProduct, iCount);
		}
		yield return new WaitForSeconds(respwnTime);
		Disabled(false);
	}

	private IEnumerator CheckOnGround()
	{
		if (!(bonuseManager.thisScript != null))
		{
			yield break;
		}
		Vector3 endForTween2 = Vector3.zero;
		bool isStarted = false;
		while (true)
		{
			RaycastHit hit;
			if (isVisible && Physics.Raycast(base.transform.position + Vector3.up, -1f * Vector3.up, out hit, 1000f, bonuseManager.thisScript.layerForSpawn))
			{
				endPos = hit.point;
				endPos.y += 0.5f;
				if (!endPos.Equals(base.transform.position) && !isStarted)
				{
					isStarted = true;
					endForTween2 = endPos;
					float time = hit.distance / 5f;
					if (tweenPosition != null)
					{
						HOTween.Kill(tweenPosition);
					}
					tweenPosition = HOTween.To(base.transform, time, new TweenParms().Prop("position", endForTween2).Ease(EaseType.Linear).OnComplete((TweenDelegate.TweenCallback)delegate
					{
						isStarted = false;
						base.transform.position = endPos;
					}));
				}
			}
			yield return new WaitForSeconds(1f);
		}
	}

	public BonuseItem RandomBonuse()
	{
		if (listSpawnBonus.Count != 0)
		{
			int num = 0;
			foreach (SpawnBonuse listSpawnBonu in listSpawnBonus)
			{
				num += listSpawnBonu.percent;
			}
			int num2 = UnityEngine.Random.Range(0, num);
			int num3 = 0;
			foreach (SpawnBonuse listSpawnBonu2 in listSpawnBonus)
			{
				num3 += listSpawnBonu2.percent;
				if (num3 >= num2)
				{
					id = listSpawnBonu2.spawnType;
					idProduct = listSpawnBonu2.idProduct;
					countItemAddMin = listSpawnBonu2.count;
					ChangeTexture();
					OnOreol();
					return this;
				}
			}
		}
		return this;
	}

	public void ChangeBonuse(int iBonuse, string iProduct, int iCount)
	{
		id = (idBonuse)iBonuse;
		idProduct = iProduct;
		countItemAddMin = iCount;
		ChangeTexture();
		OnOreol();
	}

	private void OnDestroy()
	{
		bonuseManager.thisScript.RemoveBonuseFromList(this);
	}

	public void ChangeTexture()
	{
		bonusTextures.ForEach(delegate(IDBonusGameObject x)
		{
			x.go.SetActive(false);
		});
		if (idProduct.Equals("Snowball"))
		{
			bonusTextures.Find((IDBonusGameObject t) => t.id == idBonuse.Item).go.SetActive(true);
		}
		else
		{
			bonusTextures.Find((IDBonusGameObject t) => t.id == id).go.SetActive(true);
		}
	}

	public void OnOreol()
	{
		int num = 0;
		switch (id)
		{
		case idBonuse.Ammo:
			num = 3;
			break;
		case idBonuse.Health:
			num = 2;
			break;
		case idBonuse.Jetpack:
			num = 0;
			break;
		case idBonuse.Item:
			num = 1;
			break;
		}
		for (int i = 0; i < oreol.Length; i++)
		{
			if (i != num)
			{
				oreol[i].SetActive(false);
			}
			else
			{
				oreol[i].SetActive(true);
			}
		}
		if (num > oreol.Length && oreol.Length > 0)
		{
			oreol[0].SetActive(true);
		}
	}

	public void SetAnimation(Material red, Material blue, Material orange, Material green)
	{
		for (int i = 0; i < colors.Count; i++)
		{
			switch (colors[i].type)
			{
			case TypeBonusColor.Blue:
				colors[i].renderer.material = blue;
				break;
			case TypeBonusColor.Green:
				colors[i].renderer.material = green;
				break;
			case TypeBonusColor.Orange:
				colors[i].renderer.material = orange;
				break;
			case TypeBonusColor.Red:
				colors[i].renderer.material = red;
				break;
			}
		}
	}
}
